import time
from selenium import webdriver
from selenium.webdriver.common.by import By


class Ecshop:
    def __init__(self):
        # 打开浏览器
        self.driver = webdriver.Chrome()
        # 进入Ecshop
        self.driver.get('https://ecshop.test2.shopex123.com/')
        # 最大化浏览器
        self.driver.maximize_window()

    def log_on(self):
        # 定位登录按钮并点击
        self.driver.find_element(By.XPATH, '//*[@id="ECS_MEMBERZONE"]/a[1]').click()
        # 定位登录输入框并输入内容
        time.sleep(2)
        username = self.driver.find_element(By.XPATH, '//*[@id="body"]/div[1]/div/div[1]/form/div[1]/div/input')
        username.send_keys('a525855722')
        time.sleep(2)
        password = self.driver.find_element(By.XPATH, '//*[@id="body"]/div[1]/div/div[1]/form/div[2]/div/input')
        password.send_keys('JH13277968935')
        # 定位登录按钮并点击登录
        time.sleep(2)
        sign_in = self.driver.find_element(By.CLASS_NAME, 'loginbtn')
        sign_in.click()

    def search(self):
        # 定位搜索框
        time.sleep(1)
        keyword = self.driver.find_element(By.ID, 'keyword')
        # 在搜索框输入内容
        time.sleep(1)
        keyword.send_keys('资生堂')
        # 定位并点击搜索按钮
        self.driver.find_element(By.XPATH, '//*[@id="searchForm"]/table/tbody/tr/td[2]/input').click()

    def post(self):
        # 定位免费注册按钮
        self.driver.find_element(By.XPATH, '//*[@id="ECS_MEMBERZONE"]/a[2]').click()
        # 定位所有注册输入框输入内容
        time.sleep(2)
        username = self.driver.find_element(By.ID, 'username')
        username.send_keys('a525855722')
        time.sleep(2)
        email = self.driver.find_element(By.ID, 'email')
        email.send_keys('525855722@qq.com')
        time.sleep(2)
        password = self.driver.find_element(By.ID, 'password1')
        password.send_keys('JH13277968935')
        time.sleep(2)
        confirm_password = self.driver.find_element(By.ID, 'confirm_password')
        confirm_password.send_keys('JH13277968935')
        # 定位同意协议并注册按钮并点击
        time.sleep(2)
        agree_to_register = self.driver.find_element(By.XPATH, '//*[@id="main"]/div[2]/form/div[9]/input[3]')
        agree_to_register.click()


if __name__ == '__main__':
    ecshop = Ecshop()
    # ecshop.log_on()
    # ecshop.search()
    # ecshop.post()
    time.sleep(10)
    ecshop.driver.quit()